package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.RoleMapper;
import com.kkb.mapper.RoleMenuMapper;
import com.kkb.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Role> queryByPage(Integer pageNum, Integer pageSize, String roleName) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (roleName != null && roleName.trim().length() > 0) {
            criteria.andRoleNameLike("%" + roleName.trim() + "%");
        }
        criteria.andIsDelEqualTo(0);
        PageHelper.startPage(pageNum, pageSize);
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        return new PageInfo<>(roleList, 5);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Role> queryByRoleName(String roleName) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andRoleNameEqualTo(roleName);
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        return roleList;
    }

    /**
     * 添加用户，动态获取角色列表，获取的是没有被删除的，和状态是处于启用的角色列表
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Role> queryRoleList() {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIsDelEqualTo(0);
        criteria.andRoleStateEqualTo(0);
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        return roleList;
    }
    /**
     * 三表联查
     * @param roleId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Role queryById(Integer roleId) {
        return roleMapper.queryById(roleId);
    }

    /**
     * role单表查
     * @param roleId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Role queryByRoleId(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addRole(Role role, List<Integer> menuIds) {
        role.setIsDel(0);
        int i = roleMapper.insertSelective(role);
        Integer roleId = role.getRoleId();
        //  System.out.println("roleId:"+roleId);
        for (Integer menuId : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(roleId);
            int num = roleMenuMapper.insertSelective(roleMenu);
            i = i + num;
        }
        return i;
    }

    /**
     * 更新角色
     * @param role
     * @param menuIds
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateRole(Role role,List<Integer> menuIds) {
        Integer roleId = role.getRoleId();
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria roleExampleCriteria = roleExample.createCriteria();
        roleExampleCriteria.andRoleNameEqualTo(role.getRoleName());
        roleExampleCriteria.andRoleIdNotEqualTo(roleId);
        List<Role> roleList = roleMapper.selectByExample(roleExample);
        if (roleList.size()>0) {
            return -1;
        }
        RoleMenuExample roleMenuExample = new RoleMenuExample();
        RoleMenuExample.Criteria criteria = roleMenuExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(roleMenuExample);
        int num = roleMapper.updateByPrimaryKeySelective(role);
        for (Integer menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(menuId);
                roleMenu.setRoleId(roleId);
                int num1 = roleMenuMapper.insertSelective(roleMenu);
                num+=num1;
            }
        return num;
    }

    /**
     * 删除单个角色
     * @param RoleId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteRole(Integer RoleId) {
        Role role = new Role();
        role.setRoleId(RoleId);
        role.setIsDel(1);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 删除多个role
     * @param roleIds
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteRoles(List<Integer> roleIds) {
        int i = roleMapper.deleteRoles(roleIds);
        return i;
    }
}

